<template>
  <div class="table-page">
    <a-row justify="space-between" align="center" class="header page_header">
      <a-space wrap>
        <div class="title">公司管理</div>
      </a-space>
    </a-row>
    <a-row align="stretch" :gutter="14" class="h-full page_content">
      <a-col :xs="0" :sm="8" :md="7" :lg="6" :xl="5" :xxl="4" flex="260px" class="h-full ov-hidden">
        <CompanyTree @node-click="handleSelectCompany"></CompanyTree>
      </a-col>
      <a-col :xs="24" :sm="16" :md="17" :lg="18" :xl="19" :xxl="20" flex="1" class="h-full ov-hidden">
        <GiTable row-key="id" :data="dataList" :columns="columns" :loading="loading"
          :scroll="{ x: '100%', y: '100%', minWidth: 1000 }" :pagination="pagination" :disabled-tools="['size']"
          :disabled-column-keys="['name']" @refresh="search">
          <template #toolbar-left>
            <!-- <a-button @click="reset">
              <template #icon><icon-refresh /></template>
              <template #default>重置</template>
            </a-button> -->
          </template>
          <template #toolbar-right>
            <a-button v-permission="['card:company:add']" type="primary" @click="onAdd">
              <template #icon><icon-plus /></template>
              <template #default>新增</template>
            </a-button>
            <!-- <a-button v-permission="['card:company:export']" @click="onExport">
              <template #icon><icon-download /></template>
              <template #default>导出</template>
            </a-button> -->
          </template>
          <template #action="{ record }">
            <a-space>
              <a-link v-permission="['card:company:list']" title="查看" @click="onDetail(record)">查看</a-link>
              <a-link v-permission="['card:company:update']" title="修改" @click="onUpdate(record)">修改</a-link>
              <a-link v-permission="['card:company:delete']" status="danger" :disabled="record.disabled" title="删除"
                @click="onDelete(record)">
                删除
              </a-link>
            </a-space>
          </template>
        </GiTable>
      </a-col>
    </a-row>

    <IntroductionAddModal ref="IntroductionAddModalRef" @save-success="search" />
    <IntroductionDetail ref="IntroductionDetailRef" />
  </div>
</template>

<script setup lang="ts">
import { deleteCompanyIntroduction, listCompanyIntroduction, type CompanyIntroductionResp, type CompanyQuery } from '@/apis/website/companyIntroduction'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import { Message } from '@arco-design/web-vue'
import IntroductionDetail from './detail.vue'
import IntroductionAddModal from './IntroductionAddModal.vue'
import CompanyTree from './tree/index.vue'

defineOptions({ name: 'Company' })


const queryForm = reactive<CompanyQuery>({
  companyId: undefined,
  sort: ['createTime,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listCompanyIntroduction({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstanceColumns[] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    fixed: !isMobile() ? 'left' : undefined,
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '标题', dataIndex: 'title', slotName: 'title' },
  { title: '内容', dataIndex: 'content', slotName: 'content', ellipsis: true, tooltip: false },
  { title: '描述', dataIndex: 'description', slotName: 'description' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  { title: '创建人', dataIndex: 'createUserString', slotName: 'createUserString' },
  // { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime' },
  // { title: '修改人id', dataIndex: 'updateUser', slotName: 'updateUser' },
  {
    title: '操作',
    slotName: 'action',
    width: 180,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['card:company:update', 'card:company:delete'])
  }
]

// 重置
const reset = () => {
  search()
}

// 删除
const onDelete = (record: CompanyIntroductionResp) => {
  return handleDelete(() => deleteCompanyIntroduction(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}


const IntroductionAddModalRef = ref<InstanceType<typeof IntroductionAddModal>>()
// 新增
const onAdd = (id?: string, companyId?: string) => {
  if (queryForm.companyId === undefined) {
    Message.info('请先选择公司')
    return false
  }
  
  IntroductionAddModalRef.value?.onAdd(undefined, queryForm.companyId)
}

// 修改
const onUpdate = (record: CompanyIntroductionResp) => {
  IntroductionAddModalRef.value?.onUpdate(record.id)
}

const IntroductionDetailRef = ref<InstanceType<typeof IntroductionDetail>>()
// 详情
const onDetail = (record: CompanyIntroductionResp) => {
  IntroductionDetailRef.value?.onDetail(record.id)
}

//根据选中公司查询
const handleSelectCompany = (keys: Array<any>) => {
  queryForm.companyId = keys.length === 1 ? keys[0] : undefined
  search()
}
</script>

<style lang="scss" scoped></style>
